package com.linfnn.demo.Controller;

import com.linfnn.demo.Mapper.AdminUserMapper;
import com.linfnn.demo.Model.AdminUser;
import com.linfnn.demo.Service.AdminUserService;
import com.linfnn.demo.Service.impl.AdminUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class login {
    @Autowired
    AdminUserServiceImpl adminUserServiceImpl;

    @RequestMapping("/main")
    public String main(){
        return "/selectUserMod";
    }

    @RequestMapping("/login")
    public String loginpage(){
        return "/login";
    }

    @RequestMapping("/admin")
    public String loginpageadmin(){
        return "/admin";
    }

    @RequestMapping(value = "/submit")
//    @ResponseBody 不为这个注解的时候，返回的字符串为跳转到返回的页面
    public ModelAndView login(String username, String  password){
        if (username == null || username.isEmpty()) {
            System.out.println("登录失败");
            return new ModelAndView("redirect:/login.html");
        }
        AdminUser byName = adminUserServiceImpl.getByName(username);
        if (byName != null && byName.getPassword().equals(password)) {
            System.out.println("登录成功");
            return new ModelAndView("redirect:/search/index.html");
        }
        System.out.println("登录失败");
        return new ModelAndView("redirect:/login.html");
    }

    @RequestMapping(value = "/submitAdmin")
//    @ResponseBody 不为这个注解的时候，返回的字符串为跳转到返回的页面
    public ModelAndView loginadmin(String username, String  password){
        if (username == null || username.isEmpty()) {
            System.out.println("登录失败");
            return new ModelAndView("redirect:/admin.html");
        }
        AdminUser byName = adminUserServiceImpl.getByName(username);
        if (byName != null && byName.getPassword().equals(password)) {
            System.out.println("登录成功");
            return new ModelAndView("redirect:/managehtml/manage.html");
        }
        System.out.println("登录失败");
        return new ModelAndView("redirect:/admin.html");
    }

}



